package com.hamster.crmpre.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hamster.crmpre.pojo.PotentialCustomer;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Tarry
 * @create 2019/12/21 10:34
 */
@Repository
@Mapper
public interface PotentialCustomerMapper extends BaseMapper<PotentialCustomer> {

    /**
     * 查询潜在客户列表
     * @param id
     * @return
     */
    @Select("select A.id,A.phone,A.wx_number,A.name,A.gender,B.state from potential_customer A,employee_potential_customer B where A.id = B.potential_customer_id and B.employee_id = #{id}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "phone",column = "phone"),
            @Result(property = "wxNumber",column = "wx_number"),
            @Result(property = "name",column = "name"),
            @Result(property = "gender",column = "gender"),
            @Result(property = "state",column = "state"),
            @Result(property = "participant",column = "id",many = @Many(select = "com.hamster.crmpre.mapper.PotentialCustomerMapper.selectPotentialCustomerEmployeeName")),
    })
    List<PotentialCustomer> selectPotentialCustomer(@Param("id") String id);

    @Select("select A.name from employee A,employee_potential_customer B where A.id = B.employee_id and B.state = 1 and B.potential_customer_id = #{id}")
    List<String> selectPotentialCustomerEmployeeName(@Param("id") String id);

    @Update("update potential_customer set state = 1 where phone = #{phone}")
    void updateState(@Param("phone") String phone);


    @Update("update employee_potential_customer set employee_id = #{receiverId} where employee_id = #{senderId}")
    void updateByUser(@Param("senderId") String senderId,@Param("receiverId") String receiverId);


    /**
     * 根据姓名查询潜在客户列表
     * @param id
     * @param name
     * @return
     */
    @Select("select A.id,A.phone,A.wx_number,A.name,A.gender,B.state from potential_customer A,employee_potential_customer B where A.id = B.potential_customer_id and B.employee_id = #{id} and A.name like #{name}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "phone",column = "phone"),
            @Result(property = "wxNumber",column = "wx_number"),
            @Result(property = "name",column = "name"),
            @Result(property = "gender",column = "gender"),
            @Result(property = "state",column = "state"),
            @Result(property = "participant",column = "id",many = @Many(select = "com.hamster.crmpre.mapper.PotentialCustomerMapper.selectPotentialCustomerEmployeeName")),
    })
    List<PotentialCustomer> selectPotentialCustomerByName(@Param("id") String id,@Param("name") String name);
}
